package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"takeout/api/internal/grpc_client"
	pb "takeout/server/proto"
)

// 定义接收数据的结构体
type LoginReq struct {
	// binding:"required"修饰的字段，若接收为空值，则报错，是必须字段
	Mobile   string `form:"mobile" json:"mobile" binding:"required"`
	Password string `form:"password" json:"password" binding:"required"`
	Code     int32  `form:"code" json:"code" binding:"required"`
}

func Login(c *gin.Context) {

	var req LoginReq
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"message": "参数获取失败"})
		return
	}
	login, err := grpc_client.UserClient.Login(c, &pb.LoginRequest{
		Mobile:   req.Mobile,
		Password: req.Password,
		Code:     req.Code,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "登录失败" + err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "登录成功", "data": login})

}
